

//显示登录名称
function getName() {
    let userData = localStorage.getItem('username');
    let userShow = document.querySelector('#signin-url');
    let idShow = document.querySelector('#reg-url');

    if (userData == null) {
        userShow.innerText = '[请登录]';
        idShow.innerText = '[免费注册]';
    }
    else {
        console.log(userData);
        userShow.innerText = userData;
        userShow.style.color = 'black';
        let quit = document.querySelector('#reg-url');
        quit.innerText = '[退出]';
        quit.setAttribute('href','javascript:void(0);')
    }
}
getName();

//点击退出

function exit(evt) {
    let e = evt || window.event;
    let exit = document.querySelector('#reg-url');
    console.log(exit.getAttribute('href'))
    if (exit.getAttribute('href') == 'javascript:void(0);') { 
        exit.innerText = '[免费注册]';
        e.preventDefault();
        exit.setAttribute('href', 'login.html')
        // let keys = Object.keys(localStorage);
        localStorage.removeItem('username');
        localStorage.removeItem('token')
        getName();
    }
    
}




// 下滑一定距离显示返回顶部按键
let showLayer = document.querySelector('.right-floating-layer');
window.onscroll = function () { 
    let top = document.body.scrollTop || document.documentElement.scrollTop;
    if (top >= 200) {
        showLayer.style.display = 'block';
    } else { 
        showLayer.style.display = 'none';
    }
}
//返回顶部过程
let backTop = document.getElementsByClassName('layer');
let box1 = document.querySelector('.layer');
console.log(box1)
console.log(backTop)

// backTop[0].addEventListener('click',function(e){
box1.addEventListener('click',function(e){
    var backTopTimer = setInterval(function(){
        // topH = document.documentElement.scrollTop || document.body.scrollTop;
        // backTopSpeed = Math.floor(-topH/8);//取整
        // if(topH>0){
        //     document.documentElement.scrollTop = document.body.scrollTop = topH+backTopSpeed;
        // }else{
        //     clearInterval(backTopTimer);
        // }
        topH = document.documentElement.scrollTop || document.body.scrollTop;
        let backTopSpeed = parseInt(topH - (topH / 8));
        console.log(backTopSpeed)
        if (backTopSpeed > 0) {
            document.documentElement.scrollTop = backTopSpeed;
        } else { 
            clearInterval(backTopTimer);
        }
    }, 10);
    e.preventDefault();
});





//获取数据

function getData() { 
    let uid = 114818;
	let pagenum = 0;
	let pagesize = 1000;
    let indexUrl = 'http://jx.xuzhixiang.top/ap/api/allproductlist.php';  
    let p1 = axios.get(indexUrl, { params: {uid,pagenum,pagesize}})
    p1.then((result) => { 
        console.log(result.data.data)
        let allObj = result.data.data;
        let arr = [];
        allObj.forEach((item) => { 
            let everyData = `
                <div class="good-info imgopacity">
                    <div class="tag-container"></div>
                    <div class="good-detail-img">
                        <a href="./html/item.html?id=${item.pid}" class="good-thumb">
                            <img src="${item.pimg}" alt="">
                        </a>
                    </div>
                    <div class="good-detail-text">
                        <a href="./html/item.html?id=${item.pid}">${item.pname}</a>
                        <p class="price">
                            <span class="sale-priceprime-cost">¥&nbsp${item.pprice}</span>
                        </p>
                    </div>
                </div>
            `;
            arr.push(everyData);
        })
        let everyShow = arr.join('');
        document.querySelector('.commodity').innerHTML = everyShow;
    })
}
getData()
